package com.domor.xxx.system.service;

import com.domor.xxx.common.core.domain.TreeEntity;
import com.domor.xxx.system.domain.SysDept;
import com.domor.xxx.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门管理 服务层
 * @author liyuyang
 */
@Service
public class SysDeptService {

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 根据ID获取部门
     * @param deptId
     * @return
     */
    public SysDept getById(int deptId) {
        return deptMapper.selectById(deptId);
    }

    /**
     * 根据树形结构的部门列表
     * @param status
     * @return
     */
    public List<SysDept> queryTreeGrid(int status) {
        List<SysDept> depts = deptMapper.selectList(status);
        List<SysDept> result = new ArrayList<>();
        for (SysDept dept : depts) {
            if (dept.getParentId() == 0) {
                List<SysDept> children = getChildren(depts, dept.getDeptId());
                dept.setChildren(children);
                result.add(dept);
            }
        }
        return result;
    }

    private List<SysDept> getChildren(List<SysDept> depts, int parentId) {
        List<SysDept> result = new ArrayList<>();
        for (SysDept dept : depts) {
            if (dept.getParentId() == parentId) {
                List<SysDept> children = getChildren(depts, dept.getDeptId());
                dept.setChildren(children);
                result.add(dept);
            }
        }
        return result;
    }

    /**
     * 返回部门树
     * @param status
     * @return
     */
    public List<TreeEntity> queryTree(int status) {
        List<SysDept> depts = deptMapper.selectList(status);
        List<TreeEntity> result = new ArrayList<>();
        for (SysDept dept : depts) {
            if (dept.getParentId() == 0) {
                TreeEntity tree = new TreeEntity();
                tree.setId(dept.getDeptId());
                tree.setText(dept.getDeptName());
                List<TreeEntity> chileren = getTreeChildren(depts, dept.getDeptId());
                if (chileren != null && chileren.size() > 0) {
                    tree.setChildren(chileren);
                }
                result.add(tree);
            }
        }
        return result;
    }

    private List<TreeEntity> getTreeChildren(List<SysDept> depts, int parentId) {
        List<TreeEntity> result = new ArrayList<>();
        for (SysDept dept : depts) {
            if (dept.getParentId() == parentId) {
                TreeEntity tree = new TreeEntity();
                tree.setId(dept.getDeptId());
                tree.setText(dept.getDeptName());
                tree.setChildren(getTreeChildren(depts, dept.getDeptId()));
                result.add(tree);
            }
        }
        return result;
    }

    /**
     * 增加部门
     * @param dept
     * @return
     */
    public int insert(SysDept dept) {
        return deptMapper.insert(dept);
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    public int update(SysDept dept) {
        return deptMapper.update(dept);
    }

    /**
     * 根据ID删除部门
     * @param deptId
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void delete(int deptId) {
        deptMapper.delete(deptId);
        deleteChildren(deptId);
    }

    private void deleteChildren(int parentId) {
        List<SysDept> depts = deptMapper.selectByParentId(parentId);
        for (SysDept m : depts) {
            deptMapper.delete(m.getDeptId());
            deleteChildren(m.getParentId());
        }
    }

}
